clear all
set more off

use "MPPEC Dataset.dta"

gen id = _n

preserve

bysort pair: gen diff=meeting_duration[2]-meeting_duration[1] if _n==1

do hodges.do

restore 

merge 1:1 id using treatment_comb


*** Fisher's randomization experiment

*Keele et al - subtract k from all treatment outcomes of interest for various k
*foreach num of numlist -2/75 {

*********************************************
*DIFFERENCE IN MEANS
*********************************************
*foreach num of numlist 0(.005)2  {
*preserve
*replace meeting_duration = meeting_duration -`num'  if mobilization == 1
	sort pair mobilization
	capture drop diff
	bysort pair: gen diff=meeting_duration[2]-meeting_duration[1] if _n==1
	egen avg_diff_0=mean(diff) 
	drop diff

	
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

*preserve


local nsim=128                                                   // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i'=meeting_duration[2]-meeting_duration[1] if _n==1
	egen avg_diff_`i'=mean(diff_`i') 
	drop diff_`i' mobilization_`i'

}
***
qui {
	***    Manipulating the output from the simulations
	**************************************************************************
	
	drop if _n>1
	keep avg_diff_*
	gen id=_n-1
	
	reshape long avg_diff_, i(id) j(bsrep)
	ren avg_diff_ avg_diff_sim
	drop id
	gen avg_diff=avg_diff_sim[1]
	drop if bsrep==0
	
	sort avg_diff_sim
	local ci1 =  avg_diff_sim[round(0.025*(_N),1)]
	local ci2 = avg_diff_sim[round(0.975*(_N),1)]
	local sign=sign(avg_diff[1])
	noi di `num'
	sum avg_diff_sim
	local sim = r(mean)
	sum avg_diff
	local actual = r(mean)
	if `actual'> `sim' {
		
		qui count if avg_diff_sim>avg_diff
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if avg_diff_sim<avg_diff
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}
	}

restore

}

count if  avg_diff_sim < 0
local negative = r(N)	
gen one_tailp = binomial(600,`negative',0.5)
gen two_tailp = one_tailp*2 

foreach num of numlist 1/300 {
gen ci_`num' = 1- (2 * binomial(600,`num',0.5))
}

*ci 275 has 95% coverage
*(-5.285, 40.14)
rbounds avg_diff_sim, gamma(1 (1) 4 4.25 5 6)

stop

di `ci1'
di `ci2'

gen c = tden(599, avg_diff_sim)
gen d = normal(avg_diff_sim)

*/
************************************************
*RANK SUM
************************************************

clear all
set more off

cd "C:\Users\Aletheia\Dropbox\Ryan\Kenya Local Gov Experiment\Data Archive"

use "Kenya Local Gov-August.dta"

gen id = _n

merge 1:1 id using treatment_comb
assert _merge == 3
drop _merge

*** Creating Wilcoxon Signed Rank test statistic for actual data
*foreach num of numlist 0(.005)2  {
*preserve
*replace meeting_duration = meeting_duration -`num'  if mobilization == 1
sort pair mobilization
bysort pair: gen diff=meeting_duration[2]-meeting_duration[1] if _n==1
gen abs_diff = abs(diff)
egen rank = rank(abs_diff) if abs_diff != . & abs_diff != 0
egen w_0 = sum(rank) if diff > 0
sum w_0
replace w_0 = r(mean) if w_0 == .
*sum w
*local w_value = r(mean)
*local z = (`w_value' - 14)/5.916
*egen w_0=mean(`z') if `z'~=0
drop diff-rank
	
**************************************************************************
***                     Within pairs re-assignment of treatment
**************************************************************************

* Creating Wilcoxon Signed Rank test statistic for simulated data

local nsim=128 
                                                 // define the number of simulations
qui forvalues i=1/`nsim' {
	***    Running baseline analysis (difference bw treatment and control)
	**************************************************************************
	
	sort pair mobilization_`i'
	bysort pair: gen diff_`i' =meeting_duration[2]-meeting_duration[1] if _n==1
	gen abs_diff_`i' = abs(diff_`i')
	egen rank_`i' = rank(abs_diff_`i') if abs_diff_`i' != . & abs_diff_`i' != 0
	egen w_`i' = sum(rank_`i') if diff_`i' > 0
	sum w_`i'
	replace w_`i' = r(mean) if w_`i' == .
	*local w_value = r(mean)
	*local z = (`w_value' - 14)/5.916
	*egen w_`i'=mean(`z') if `z'~=0
	
	*drop diff_`i'-rank_`i' mobilization_`i'
}

*qui {
drop if _n>1
	keep w_*
	gen id=_n-1
	
	reshape long w_, i(id) j(bsrep)
	ren w_ w_sim
	drop id
	gen w=w_sim[1]
	drop if bsrep==0
	*drop if w_sim == .
	sort w_sim
	
	gen z_sim = (w_sim - 14)/5.916
	gen z = (w - 14)/5.916
	sort z_sim
	local ci1 =  w_sim[round(0.025*(_N),1)]
	local ci2 = w_sim[round(0.975*(_N),1)]
	local ci1_b =  z_sim[round(0.025*(_N),1)]
	local ci2_b = z_sim[round(0.975*(_N),1)]
	local sign=sign(z)
	noi di `num'
	sum w_sim
	local sim = r(mean)
	sum w
	local actual = r(mean)
	if `actual'> `sim' {
		
		qui count if w_sim>w
		local greater=r(N)
		
		
		*** P-value calculation for one-tailed hypothesis tests
				
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`greater'/_N'
		
}
	
	else {

		qui count if w_sim<w
		local smaller=r(N)
		
		
		
		*** P-value calculation for one-tailed hypothesis tests
			
		noi di "The p-value of the one-tailed test of the null hypothesis that the mean difference between Treatment and Control is 0 is:  " `=`smaller'/_N'
		
		}

restore
}

